<template>
  <div class="add-card">
    <header class="add-header">
      <el-page-header :content="title" @back="$router.back()" />
    </header>
    <main class="add-main">
      <div class="form-container">
        <div class="title">车辆信息</div>
        <div class="form">
          <el-form ref="carForm" label-width="100px" :model="carInfoForm" :rules="carRules">
            <el-form-item label="车主姓名" prop="personName">
              <el-input v-model="carInfoForm.personName" />
            </el-form-item>
            <el-form-item label="联系方式" prop="phoneNumber">
              <el-input v-model="carInfoForm.phoneNumber" />
            </el-form-item>
            <el-form-item label="车辆号码" prop="carNumber">
              <el-input v-model="carInfoForm.carNumber" />
            </el-form-item>
            <el-form-item label="车辆品牌" prop="carBrand">
              <el-input v-model="carInfoForm.carBrand" />
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div class="form-container">
        <div class="title">最新一次月卡缴费信息</div>
        <div class="form">
          <el-form ref="cardForm" label-width="100px" :model="cardFrom" :rules="cardRules">
            <el-form-item label="有效日期" prop="cardStartDate">
              <el-date-picker
                v-model="dateForm"
                value-format="yyyy-MM-dd"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                @change="onDateChange"
              />
            </el-form-item>
            <el-form-item label="支付金额" prop="paymentAmount">
              <el-input v-model.number="cardFrom.paymentAmount" />
            </el-form-item>
            <el-form-item label="支付方式" prop="paymentMethod">
              <el-select v-model="cardFrom.paymentMethod">
                <el-option v-for="item in payMethod" :key="item.id" :value="item.value" :label="item.name" />
              </el-select>
            </el-form-item>
          </el-form>
        </div>
      </div>

    </main>
    <footer class="add-footer">
      <div class="btn-container">
        <el-button v-if="cardID" @click="onResetForm">重置</el-button>
        <el-button type="primary" @click="onSubmitForm">确定</el-button>
      </div>
    </footer>
  </div>
</template>

<script>
import { addCardAPI, getCardDetailAPI, updateCardAPI } from '@/api/card'
export default {
  name: 'AddCard',
  data() {
    return {
      cardID: null,
      carInfoForm: {
        carInfoId: null,
        personName: '',
        phoneNumber: '',
        carNumber: '',
        carBrand: ''
      },
      cardFrom: {
        rechargeId: null,
        cardStartDate: '',
        cardEndDate: '',
        paymentAmount: '',
        paymentMethod: ''
      },
      carRules: {
        personName: [
          { required: true, message: '请输入车主姓名', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        ],
        phoneNumber: [
          { required: true, message: '请输入联系方式', trigger: 'blur' },
          { pattern: /^1[34578]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
        ],
        carNumber: [
          { required: true, message: '请输入车辆号码', trigger: 'blur' },
          { min: 7, max: 10, message: '长度在 7 到 10 个字符', trigger: 'blur' }
        ],
        carBrand: [
          { required: true, message: '请输入车辆品牌', trigger: 'blur' },
          { max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        ]
      },
      cardRules: {
        cardStartDate: [
          { required: true, message: '请选择有效日期', trigger: ['change', 'blur'] }
        ],
        cardEndDate: [
          { required: true, message: '请选择有效日期', trigger: ['change', 'blur'] }
        ],
        paymentAmount: [
          { required: true, message: '请输入支付金额', trigger: 'blur' },
          { type: 'number', message: '请输入数字', trigger: 'blur' }
        ],
        paymentMethod: [
          { required: true, message: '请选择支付方式', trigger: 'change' }
        ]
      },
      dateForm: ['', ''],
      payMethod: [
        { id: 1, name: '支付宝', value: 'Alipay' },
        { id: 2, name: '微信', value: 'WeChat' },
        { id: 3, name: '线下', value: 'Cash' }
      ]
    }
  },
  computed: {
    title() {
      return this.cardID ? '编辑月卡' : '增加月卡'
    }
  },
  created() {
    this.cardID = this.$route.query.id || null
    if (this.cardID) {
      this.getCardDetail()
    }
  },
  methods: {
    onDateChange() {
      this.cardFrom.cardStartDate = ''
      this.cardFrom.cardEndDate = ''
      this.cardFrom.cardStartDate = this.dateForm[0]
      this.cardFrom.cardEndDate = this.dateForm[1]
    },
    async onSubmitForm() {
      await this.$refs.carForm.validate()
      await this.$refs.cardForm.validate()
      if (this.cardID) {
        // 编辑
        await updateCardAPI({
          ...this.carInfoForm,
          ...this.cardFrom
        })
      } else {
        // 新增
        await addCardAPI({
          ...this.carInfoForm,
          ...this.cardFrom
        })
      }
      this.$message.success(this.cardID ? '修改成功' : '添加成功')
      this.$router.back()
    },
    onResetForm() {
      this.$refs.carForm.resetFields()
      this.$refs.cardForm.resetFields()
      this.dateForm = ['', '']
    },
    async getCardDetail() {
      const res = await getCardDetailAPI(this.cardID)

      const { carInfoId, personName, phoneNumber, carNumber, carBrand } = res.data
      this.carInfoForm = { carInfoId, personName, phoneNumber, carNumber, carBrand }
      const { rechargeId, cardStartDate, cardEndDate, paymentAmount, paymentMethod } = res.data
      this.cardFrom = { rechargeId, cardStartDate, cardEndDate, paymentAmount, paymentMethod }
      this.dateForm = [cardStartDate, cardEndDate]
    }
  }
}
</script>

<style scoped lang="scss">
.add-card {
  background-color: #f4f6f8;
  height: 100vh;

  .add-header {
    display: flex;
    align-items: center;
    padding: 0 20px;
    height: 64px;
    background-color: #fff;

    .left {
      span {
        margin-right: 4px;
      }

      .arrow {
        cursor: pointer;
      }
    }

    .right {
      text-align: right;
    }
  }

  .add-main {
    background: #f4f6f8;
    padding: 20px 130px;

    .form-container {
      background-color: #fff;

      .title {
        height: 60px;
        line-height: 60px;
        padding-left: 20px;
      }

      .form {
        margin-bottom: 20px;
        padding: 20px 65px 24px;

        .el-form {
          display: flex;
          flex-wrap: wrap;

          .el-form-item {
            width: 50%;
          }
        }
      }
    }

    .preview {
      img {
        width: 100px;
      }
    }
  }

  .add-footer {
    position: fixed;
    bottom: 0;
    width: 100%;
    padding: 24px 50px;
    color: #000000d9;
    font-size: 14px;
    background: #fff;
    text-align: center;
  }
}
</style>
